package admin.service.impl;

import admin.entity.Checkgroup;
import admin.util.PageQuery;
import admin.common.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import admin.mapper.CheckgroupMapper;
import admin.mapper.CheckgroupcheckitemMapper;
import admin.entity.Checkgroupcheckitem;
import admin.service.CheckgroupService;
import admin.util.PageResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CheckgroupServiceImpl implements CheckgroupService {

    @Resource
    private CheckgroupMapper checkgroupMapper;

    @Resource
    private CheckgroupcheckitemMapper checkgroupcheckitemMapper;

    @Override
    public PageResult findPageInfo(PageQuery pageQuery){
        //判断是否携带模糊查询的条件
        String queryString = pageQuery.getQueryString();
        //条件查询用LambdaQueryWrapper
        LambdaQueryWrapper<Checkgroup> lambdaQueryWrapper = null;/*简化代码*/
        if(queryString!=null && queryString.length()>0){
            lambdaQueryWrapper = new LambdaQueryWrapper<>(); //条件构造器
            //like模糊查询 用code 或者 name 做模糊检索
            lambdaQueryWrapper.like(Checkgroup::getCode,queryString);
            lambdaQueryWrapper.or();//两个条件是 或 的关系
            lambdaQueryWrapper.like(Checkgroup::getName,queryString);
        }


        //当前页 每页显示的条数
        Page<Checkgroup> page = new Page<>(pageQuery.getCurrentPage()
                , pageQuery.getPageSize());
        checkgroupMapper.selectPage(page,lambdaQueryWrapper);

        return new PageResult(page.getTotal(),page.getRecords());
    }

    @Override
    public Result saveGroupInfo(Integer[] ids, Checkgroup checkgroup){
        //保存检查组信息
        boolean flag = false;
        //保存检查组信息
        flag = checkgroupMapper.insert(checkgroup)>0;
        //保存中间表数据
        for(Integer itemId : ids){
            Checkgroupcheckitem checkgroupcheckitem = new Checkgroupcheckitem();
            checkgroupcheckitem.setCheckgroupId(checkgroup.getId());
            checkgroupcheckitem.setCheckitemId(itemId);
            //保存中间表数据
            flag = checkgroupcheckitemMapper.insert(checkgroupcheckitem)>0;
        }
        if(flag){
            return Result.ok();
        }
        else{
            return Result.fail();
        }

    }

    //根据检查组的id获取检查项的id（至少1个）
    @Override
    public Result getCheckitemids(Integer id){
        //条件构造器
        LambdaQueryWrapper<Checkgroupcheckitem> wrapper =
                new LambdaQueryWrapper<>();
        wrapper.eq(Checkgroupcheckitem::getCheckgroupId,id);
        List<Checkgroupcheckitem> list = checkgroupcheckitemMapper.selectList(wrapper);
        List<Integer> ids = new ArrayList<>();
        //遍历集合将查询到的检查项id加入到集合中
        for(Checkgroupcheckitem one : list){
            ids.add(one.getCheckitemId());
        }
        return Result.ok().data(ids);
    }

    @Override
    public Result updateGroupInfo(Integer[] ids, Checkgroup checkgroup){
        //更新检查组的信息
        boolean flag = false;
        flag = checkgroupMapper.updateById(checkgroup)>0;
        //从中间表删除原有的对应关系
        LambdaQueryWrapper<Checkgroupcheckitem> wrapper =
                new LambdaQueryWrapper<>();
        wrapper.eq(Checkgroupcheckitem::getCheckgroupId,checkgroup.getId());

        flag = checkgroupcheckitemMapper.delete(wrapper)>0;
        //将新的关系添加到表里
        for(Integer itemId : ids){
            Checkgroupcheckitem checkgroupcheckitem = new Checkgroupcheckitem();
            checkgroupcheckitem.setCheckgroupId(checkgroup.getId());
            checkgroupcheckitem.setCheckitemId(itemId);
            //保存中间表数据
            flag = checkgroupcheckitemMapper.
                    insert(checkgroupcheckitem)>0;
        }
        if(flag){
            return Result.ok();
        }
        else {
            return Result.fail();
        }

    }

    //删除检查组的方法
    @Override
    public Result deleteInfoById(Integer id){
        boolean flag = false;
        //删除检查组的对应关系表数据
        LambdaQueryWrapper<Checkgroupcheckitem> wrapper =
                new LambdaQueryWrapper<>();
        wrapper.eq(Checkgroupcheckitem::getCheckgroupId,id);
        flag = checkgroupcheckitemMapper.delete(wrapper)>0;

        //删除检查组数据
        flag = checkgroupMapper.deleteById(id)>0;
        if(flag){
            return Result.ok();
        }
        else {
            return Result.fail();
        }
    }
}
